//
//  SearchHeaderView.m
//  IconMaster
//
//  Created by mac on 2017/7/28.
//  Copyright © 2017年 yangweiwei. All rights reserved.
//

#import "SearchHeaderView.h"

@implementation SearchHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier andArr:(NSArray *)arr{
    
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = [UIColor clearColor];
        
        UIView *view0 = [[UIView alloc] initWithFrame:CGRectMake(8, 8, KWidth - 16, 50)];
        [self.contentView addSubview:view0];
        UIImageView *imageView0 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10, 30, 30)];
//        imageView0.backgroundColor = [UIColor whiteColor];
        imageView0.image = [UIImage imageNamed:@"Hot"];
        [view0 addSubview:imageView0];
        [imageView0 sizeToFit];
        
        UILabel *titleLab = [[UILabel alloc] init];
        titleLab.text = NSLocalizedString(@"热门搜索", nil);
        titleLab.textColor = [UIColor grayColor];
        [view0 addSubview:titleLab];
        titleLab.sd_layout.leftSpaceToView(imageView0, 8).topSpaceToView(view0, 10).heightIs(30).widthIs(120);
        imageView0.sd_layout.centerYEqualToView(titleLab);
        CGFloat w = 0;//保存前一个button的宽以及前一个button距离屏幕边缘的距离
        CGFloat h = CGRectGetMaxY(view0.frame)+8;//用来控制button距离父视图的高
        for (int i = 0; i < arr.count; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.tag = 100 + i;
            button.layer.borderColor = Color(154, 211, 220).CGColor;
            button.layer.borderWidth = 1;
//            button.backgroundColor = Color(154, 211, 220);
            [button addTarget:self action:@selector(handleClick:) forControlEvents:UIControlEventTouchUpInside];
            [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            //根据计算文字的大小
            
            NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:12]};
            CGFloat length = [arr[i][@"keyword"] boundingRectWithSize:CGSizeMake(KWidth, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
            //为button赋值
            [button setTitle:NSLocalizedString(arr[i][@"keyword"], nil) forState:UIControlStateNormal];
            //设置button的frame
            button.frame = CGRectMake(20 + w, h, length + 15 + 10 , 30);
            //当button的位置超出屏幕边缘时换行 320 只是button所在父视图的宽度
            if(20 + w + length + 15 + 10 > KWidth){
                w = 0; //换行时将w置为0
                h = h + button.frame.size.height + 10;//距离父视图也变化
                button.frame = CGRectMake(20 + w, h, length + 15 + 10, 30);//重设button的frame
            }
            w = button.frame.size.width + button.frame.origin.x;
            [self.contentView addSubview:button];
        }
//        view0.frame = CGRectMake(0, 20, KWidth, h + 50);
//        return view0;
        
    }
    return self;
}
- (void)handleClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(clickSearchButton:)]) {
        [self.delegate clickSearchButton:btn];
    }
//    UIButton *button = btn;
//    NSLog(@"%@", button.titleLabel.text);
}


@end
